#include "engine.h"
#include "entityfactory.h"

#include "entityfactory.h"


//////////////////////////////////////////////////////////////////////////
//		property descriptor
//////////////////////////////////////////////////////////////////////////





//////////////////////////////////////////////////////////////////////////
//		entity metaobject
//////////////////////////////////////////////////////////////////////////
#ifdef LEO_DESIGNER
string entityfactory_t::to_string(void* entity)
{
	string s;
	s+="\"";
	s+=this->get_typename().c_str();
	s+="\" :\n{\n";


	bool kellvesszo=false;

	for (entityfactory_t* mo=this; mo; mo=mo->parent)
	{
		for (unsigned int n=0; n<mo->get_propertycount(); ++n)
		{
			property_t* p=mo->get_property(n);
			if (p->m_editoronly)
				continue;

			if (kellvesszo)
				s+=",\n";
			else
				kellvesszo=true;
			s+=p->to_string(entity);
		}
	}

	return s;
}
#endif

entityfactory_t* factorymanager_t::m_metaobjectlist=NULL;
int factorymanager_t::m_type_count=0;


entityfactory_t::entityfactory_t(stringhash i_typename, entityfactory_t* i_parent,const char* i_icon_name):
type_name(i_typename),
parent(i_parent),
#ifdef LEO_DESIGNER
icon_name(i_icon_name),
#endif
type_id(factorymanager_t::get_typeid())
{
#ifdef LEO_DESIGNER
	name_index=0;
	//	if (!icon_name)
	//		icon_name="Icons\\object.png";
#endif
	child=NULL;
	next_sibling=NULL;
	factorymanager_t::add_metaobject(this);
}



//////////////////////////////////////////////////////////////////////////
// metaobject manager
//////////////////////////////////////////////////////////////////////////
#ifdef LEO_DESIGNER
#include "file/file.h"
#endif

void factorymanager_t::init()
{
#ifdef LEO_DESIGNER
	file_t f;
	f.open("entity_list.txt","wb");
#endif
	for (entityfactory_t* ptr=m_metaobjectlist; ptr; ptr=ptr->next)
	{
		if (ptr->parent)
		{
			entityfactory_t* parent=ptr->parent;
			ptr->next_sibling=parent->child;
			parent->child=ptr;

#if 0//def LEO_DESIGNER
			if (!ptr->icon_name)
			{
				for (; parent && !parent->icon_name;parent=parent->parent){}

				if (parent)
					ptr->icon_name=parent->icon_name;
			}
#endif

		}
#ifdef LEO_DESIGNER
		f.writestring(string(ptr->type_name.c_str())+"\n");
#endif
	}
#ifdef LEO_DESIGNER
	f.close();
#endif
}
